import { userStore } from "@/stores/user";
import type { Directive } from "vue";
export const permission: Directive = {
  mounted(el, binding) {
    // 权限校验
    const store = userStore();
    // 权限列表
    const { value } = binding;

    const permissions = store.getCodeList;
    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value;
      const hasPermission = permissions.some((role) => {
        return permissionRoles.includes(role);
      });

      if (!hasPermission) {
        el.style.display = "none";
      }
    } else {
      throw new Error("need roles! Like v-permission=\"['admin','editor']\"");
    }
  },
};
